package com.sinya.shoppingmall.fragment;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.BaseResult;
import com.sinya.shoppingmall.bean.RecommendBean;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;
import com.sinya.shoppingmall.ui.recomend.RecomendListView;
import com.sinya.shoppingmall.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌推荐
 *
 * @author KoizumiSinya
 * @date 2015/12/24. 23:54
 */

public class RecommendFragment extends BaseFragment {

    private TextView tv_title;
    private List<RecomendListView> views;
    private PagerSlidingTabStrip tabStrip;
    private ViewPager viewPager;
    private ViewPageAdapter adapter;

    private List<RecommendBean> recommendBeans;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {

                case 0:
                    if (msg.obj != null) {
                        recommendBeans = ((BaseResult) msg.obj).brand;
                        if (recommendBeans != null) {
                            createItemViews(recommendBeans);
                        }
                    }

                    break;
                default:
                    break;
            }

            return true;
        }
    });

    @Override
    protected View initView() {
        return View.inflate(mContext, R.layout.fragment_two, null);
    }

    @Override
    protected void setView() {
        initViews();
        getData();

    }

    @Override
    protected void setTitle() {
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        tv_title.setText(mContext.getString(R.string.title_brand));
    }

    private void getData() {
        VolleyRequest.Start(mContext, handler, 0, VolleyParams.brand);
    }

    private void initViews() {
        tabStrip = (PagerSlidingTabStrip) rootView.findViewById(R.id.scroll_tab);
        tabStrip.setSlidingLose(mSlidingPaneLayout);
        //tabStrip.setOnTouchListener(touchListener);
        viewPager = (ViewPager) rootView.findViewById(R.id.view_group);
        viewPager.setOnTouchListener(touchListener);
    }

    private void createItemViews(List<RecommendBean> beans) {
            views = new ArrayList<>();
            String[] arrs = new String[beans.size()];

            for (int i = 0; i < beans.size(); i++) {
                arrs[i] = beans.get(i).key;
                RecomendListView view;
                if (i == 0) {
                    view = new RecomendListView(mContext, null, beans.get(i).id, beans.get(i));
                } else {
                    view = new RecomendListView(mContext, mSlidingPaneLayout, beans.get(i).id, beans.get(i));

                }
            views.add(view);
        }

        adapter = new ViewPageAdapter(views, arrs);
        viewPager.setAdapter(adapter);
        tabStrip.setViewPager(viewPager);
        tabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //views.get(position).getData();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private OnTouchListener touchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int act = event.getAction();
            if (act == MotionEvent.ACTION_DOWN) {
                // 处理侧滑冲突的问题
                if (mSlidingPaneLayout != null) {
                    mSlidingPaneLayout.requestDisallowInterceptTouchEvent(true);
                }

            } /*else if (act == MotionEvent.ACTION_UP) {
                // 处理侧滑冲突的问题
                if (mSlidingPaneLayout != null) {
                    mSlidingPaneLayout.requestDisallowInterceptTouchEvent(false);
                }
            }*/

            return false;
        }
    };

    // [+] Adapter

    private class ViewPageAdapter extends PagerAdapter {

        private List<RecomendListView> views;
        private String[] titles;

        public ViewPageAdapter(List<RecomendListView> views, String[] titles) {
            this.views = views;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position % titles.length];
        }

        /*@Override
        public int getPageSelectedIconResId(int position) {
            return icons[position % icons.length];
        }

        @Override
        public int getTextSelectColor(int position) {
            return textSelectors[position % textSelectors.length];
        }*/
    }


    // [-] Adapter
}
